Skip to content

[Web] Include emscripten headers by default #105800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

adamscott
Copy link
Member

@adamscott adamscott commented Apr 26, 2025

Currently, developing using Emscripten for the Web platform is kinda a pain. As Emscripten uses by default it's own patched and new headers, the IDEs usually don't know what to do with Emscripten code because including manually those headers is not required.

Capture d’écran, le 2025-04-26 à 13 59 12

This code compiles with Emscripten even if clangd complains.

So this PR adds a new option: emscripten_include_path. When added, this path will be included as an include path in the compilation.

Edit: This PR now detects and adds the include path automatically, based on the detected binary.

Capture d’écran, le 2025-04-26 à 14 09 15

See how great it looks?

Note

If you're using emsdk:
scons platform=web emscripten_include_path=<emsdk path>/upstream/emscripten/cache/sysroot/include (not needed anymore)

If you're using directly emscripten:
scons platform=web emscripten_include_path=<emscripten path>/cache/sysroot/include (not needed anymore)

@dsnopek
Copy link
Contributor

dsnopek commented Apr 28, 2025

So, the goal is to get this include path into the compile_commands.json?

I wonder if there's a way to get Emscripten to give us the its include paths without the user having to specify them?

ChatGPT gave me this which seems to work:

emcc -v -E -x c++ - < /dev/null 2>&1 | awk '/#include <...> search starts here:/,/End of search list./ {if ($0 !~ /(#include|End of search)/) print $1}'

@akien-mga
Copy link
Member

Yeah I agree we should set this automatically and not require passing it on the command line.

A correct (current) Emscripten setup should have EMSDK in the environment I believe, so you can just append $EMSDK/upstream/emscripten/cache/sysroot/include/ to CPPPATH.

@adamscott
Copy link
Member Author

Yeah I agree we should set this automatically and not require passing it on the command line.

A correct (current) Emscripten setup should have EMSDK in the environment I believe, so you can just append $EMSDK/upstream/emscripten/cache/sysroot/include/ to CPPPATH.

Not necessarily. You can setup it to work directly from the emscripten-core/emscripten repo.

@adamscott adamscott force-pushed the add-emscripten-include-path branch from 0e40f5b to 657945b Compare April 29, 2025 22:30
@adamscott adamscott changed the title [Web] Add emscripten_include_path option [Web] Include emscripten headers by default Apr 29, 2025
@adamscott adamscott force-pushed the add-emscripten-include-path branch from 657945b to d3f722e Compare May 20, 2025 13:21
Comment on lines +117 to +120
emcc_path = Path(str(WhereIs("emcc")))
while emcc_path.is_symlink():
# For some reason, mypy trips on `Path.readlink` not being defined, somehow.
emcc_path = emcc_path.readlink() # type: ignore[attr-defined]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idea why mypy would be unhappy with this @Repiteo ?

# For some reason, mypy trips on `Path.readlink` not being defined, somehow.
emcc_path = emcc_path.readlink() # type: ignore[attr-defined]
emscripten_include_path = emcc_path.parent.joinpath("cache", "sysroot", "include")
env.Append(CPPPATH=[emscripten_include_path])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could maybe use CPPEXTPATH (see #104893).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It fails to build if I use CPPEXTPATH though.

[  1%] In file included from platform/web/audio_driver_web.cpp:31:
In file included from platform/web/audio_driver_web.h:33:
In file included from platform/web/godot_audio.h:33:
/home/adamscott/dev/apps/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/cstdint:148:5: error: <cstdint> tried including <stdint.h> but didn't find libc++'s <stdint.h> header.           This usually means that your header search paths are not configured properly.           The header search paths should contain the C++ Standard Library headers before           any C Standard Library, and you are probably using compiler flags that make that           not be the case.
  148 | #   error <cstdint> tried including <stdint.h> but didn't find libc++'s <stdint.h> header. \
      |     ^

@adamscott adamscott force-pushed the add-emscripten-include-path branch from d3f722e to b0e472c Compare May 20, 2025 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants